package ucadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/userkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserListLogic {
	return &QueryUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理后台查询用户列表
func (l *QueryUserListLogic) QueryUserList(in *pb.QueryUserListReq) (*pb.QueryUserListResp, error) {
	defer utils.RecoverPanic()

	var list []*usercenter.UserAccount
	var rsp pb.QueryUserListResp
	rsp.List = make([]*pb.User, 0)
	if in.Uid == 0 {
		var start, end *time.Time
		if in.RegStartTime != "" {
			loc, _ := time.LoadLocation(dbkey.DateTimeZone)
			t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.RegStartTime, loc)
			if err != nil {
				l.Logger.Errorf("err == ", zap.Error(err))
				return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
			}
			start = &t
		}
		if in.RegEndTime != "" {
			loc, _ := time.LoadLocation(dbkey.DateTimeZone)
			t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.RegEndTime, loc)
			if err != nil {
				l.Logger.Errorf("err == ", zap.Error(err))
				return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
			}
			end = &t
		}
		var err error
		list, err = l.svcCtx.UserAccountModel.Find(l.ctx, "", start, end, in.PageNo, in.PageSize)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		if len(list) <= 0 {
			return &rsp, nil
		}

		if in.PageNo <= 1 {
			rsp.Total, err = l.svcCtx.UserAccountModel.Count(l.ctx, start, end)
			if err != nil {
				l.Logger.Errorf("err == ", zap.Error(err))
				return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
			}
		}
	} else {
		uac, err := l.svcCtx.UserAccountModel.FindOne(l.ctx, in.Uid)
		if err != nil {
			if err == usercenter.ErrNotFound {
				return &rsp, nil
			}
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		list = append(list, uac)
	}

	for i := 0; i < len(list); i++ {
		var err error
		var vl pb.User
		vl.Uid = list[i].Uid
		vl.CreateTime = list[i].CreateTime.Format(dbkey.DateTimeCNFormat)
		vl.RegIp = list[i].RegIp
		vl.RegDevice = list[i].RegDevice
		vl.AccountState = list[i].AccountState
		vl.Username = list[i].AuthKey
		// 封禁
		if vl.AccountState == userkey.AccountStateBan {
			var ub *usercenter.UserBan
			ub, err = l.svcCtx.UserBanModel.FindOne(l.ctx, list[i].Uid)
			if err != nil && err != usercenter.ErrNotFound {
				l.Logger.Errorf("err == ", zap.Error(err))
				return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
			}
			if ub != nil {
				vl.AutoUnbanTime = ub.AutoUnbanTime.Format(dbkey.DateTimeCNFormat)
				vl.BanReason = ub.BanReason
			}
		}

		// 登陆信息
		var uls *usercenter.UserLoginStat
		uls, err = l.svcCtx.UserLoginStatModel.FindOne(l.ctx, list[i].Uid)
		if err != nil && err != usercenter.ErrNotFound {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		if uls != nil {
			vl.LastLoginTime = uls.LoginTime.Format(dbkey.DateTimeCNFormat)
			vl.LastLoginIp = uls.LoginIp
			vl.LastLoginDevice = uls.LoginDevice
		}

		rsp.List = append(rsp.List, &vl)
	}

	return &rsp, nil
}
